package com.zhuiyun.educations.api.record.service.impl;

import com.github.pagehelper.PageHelper;
import com.zhuiyun.educations.api.record.entity.Record;
import com.zhuiyun.educations.api.record.mapper.RecordMapper;
import com.zhuiyun.educations.api.record.service.RecordService;
import com.zhuiyun.educations.common.response.CommonResult;
import com.zhuiyun.educations.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.educations.util.pageUtil.PageRequest;
import com.zhuiyun.educations.util.pageUtil.PageResult;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName RecordServiceImpl
 * @Description TODO 开通会员管理
 * @Author ys
 * @Date 2023/6/20 14:15
 **/
@Service
public class RecordServiceImpl implements RecordService {
    @Resource
    RecordMapper recordMapper;
    /**
     * 分页查询
     * @param pageRequest
     * @return
     */
    @Override
    public CommonResult getRecord(PageRequest pageRequest) {
        PageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(recordMapper.getRecord(pageRequest));
        return CommonResult.ok(pageResult);
    }
    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @Override
    public CommonResult deleteRecordById(int id) {
        return CommonResult.ok(recordMapper.deleteRecordById(id));
    }
    /**
     * 新增
     * @param
     * @return
     */
    @Override
    public CommonResult addRecord(Record record) {
        Date date = new Date();
        record.setOpenTime(date);
        record.setCloseTime(date);
        record.setDelFlag(0);
        int months = Integer.parseInt(record.getNumber());; // 获取延期的月份值=开通会员的数量
        Date originalCloseTime = record.getCloseTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(originalCloseTime);//设置初始值时间

        switch (record.getMemberId()) {
            case 1:
                calendar.add(Calendar.MONTH, months);
                break;
            case 2:
                calendar.add(Calendar.MONTH, months * 3);
                break;
            case 3:
                calendar.add(Calendar.MONTH, months * 12);
                break;
            default:
                // 其他情况不做处理
                break;
        }

        Date newCloseTime = calendar.getTime();
        record.setCloseTime(newCloseTime);//将修改后的时间赋值回去
        return CommonResult.ok(recordMapper.insertSelective(record));
    }
    /**
     * 开通用户动态下拉
     * @param
     * @return
     */
    @Override
    public CommonResult userSelect() {
        return CommonResult.ok(recordMapper.userSelect());
    }
    /**
     * 会员类型动态下拉
     * @param
     * @return
     */
    @Override
    public CommonResult memberSelect() {
        return CommonResult.ok(recordMapper.memberSelect());
    }

}
